package com.luocj.elephant.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.luocj.elephant.demo.Bean.LineChartBean;
import com.luocj.elephant.demo.utils.Utils;

import java.util.ArrayList;

public class StatusCodeView extends View {
    private static final String TAG = StatusCodeView.class.getSimpleName();
    private Paint mPaint;
    private ArrayList<LineChartBean> beans;
    private int width;
    private int height;
    private int xOrigin;
    private int yOrigin;
    private boolean isBezierLine = true;
    private Paint bgPaint;
    private int screenWidth;

    public StatusCodeView(Context context) {
        this(context, null);
    }

    public StatusCodeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatusCodeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context mContext) {
        getDatas();
        screenWidth = Utils.getScreenWidth(mContext) -150;
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(Utils.dp2px(mContext, 15));
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setAntiAlias(true);

        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = Utils.getScreenWidth(getContext());
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = Utils.dip2px(getContext(), 200);
        }

        setMeasuredDimension(width, height);

    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        xOrigin = left + Utils.dp2px(getContext(), 20);
        yOrigin = bottom - Utils.dp2px(getContext(), 20);
        Log.i(TAG, "onLayout: " + "left -> " + left + ",top -> " + top + ",right -> " + right + ",bottom -> " + bottom);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i(TAG, "onSizeChanged: " + "w->" + w + ",h->" + h + ",oldw->" + oldw + ",oldh->" + oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.parseColor("#44FF0000"));
        canvas.translate(xOrigin, yOrigin);

        drawStatusCode(canvas);
    }

    private void drawStatusCode(Canvas canvas) {
        int stepWidth = screenWidth / (beans.size() - 1);
        for (int i = 0; i < beans.size(); i++) {
            LineChartBean lineChartBean = beans.get(i);
            canvas.drawPoint(i * stepWidth,-(lineChartBean.getyAxis()/height * Utils.dp2px(getContext(),height)),mPaint);

            canvas.drawLine(i * stepWidth,0,i* stepWidth,-(lineChartBean.getyAxis()/height * Utils.dp2px(getContext(),height)),mPaint);
        }
    }

    public ArrayList<LineChartBean> getDatas() {
        beans = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            LineChartBean lineChartBean = new LineChartBean();
            lineChartBean.setxAxis(i);
            lineChartBean.setyAxis((float) (Math.random() * 300));
            beans.add(lineChartBean);
        }
        return beans;
    }

    public void updata(LineChartBean lineChartBean) {
        beans.add(lineChartBean);
        beans.remove(0);
        bgPaint.reset();

        requestLayout();
        postInvalidate();
    }
}
